import { FILL_PAINT, LINE_LAYOUT, LINE_PAINT } from "./conf";

export const PLG_MODES = [
	"draw_attack_arrow",
	"draw_double_arrow",
	"draw_three_arrow",
	"draw_circle",
	"draw_ellipse",
	"draw_rectangle",
	"draw_polygon",
	"draw_gathering_place",
	"draw_straight_arrow",
];

export function generatePolygonLayer({ map, mode, features }) {
	features.forEach((feature) => {
		feature.properties = {
			...FILL_PAINT,
			...LINE_PAINT,
			...LINE_LAYOUT,
			...feature.properties,
		};
	});

	const fillLayer = {
		id: `${mode}-${features[0].id}-fill`,
		type: "fill",
		source: {
			type: "geojson",
			data: {
				type: "FeatureCollection",
				features,
			},
		},
		paint: {
			"fill-color": ["get", "fillColor"],
			"fill-opacity": ["get", "fillOpacity"],
		},
	};

	const strokeLayer = {
		id: `${mode}-${features[0].id}-line`,
		type: "line",
		source: {
			type: "geojson",
			data: {
				type: "FeatureCollection",
				features,
			},
		},
		layout: {
			"line-cap": ["get", "lineCap"],
			"line-join": ["get", "lineJoin"],
		},
		paint: {
			"line-color": ["get", "strokeColor"],
			"line-width": ["get", "strokeWidth"],
			"line-dasharray": ["get", "dashArray"],
		},
	};

	map.addLayer(fillLayer);
	map.addLayer(strokeLayer);

	return [fillLayer.id, strokeLayer.id];
}

export function isPolygonMode(mode) {
	return PLG_MODES.includes(mode);
}
